<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\ActivityClassif;
use App\Models\Activity;
use App\Models\ActivityJoin;
use App\Models\UsersWechat;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\ActivityExport;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Cache;

class ActivityController extends Controller
{
    public function __construct()
    {
        $this->middleware('admin');
    }

    public function index(Request $request){
        //讲定时发布的活动符合时间设置转成发布状态
        Activity::where('status',10)->where('publish_time','<',date('Y-m-d H:i:s'))->update(['status'=>1]);
        //一结束的活动 更改为status=10
        // Activity::where('end_time','<',date('Y/m/d H:i:s',time()))->where('status','<',10)->update(['status'=>0]);
        $data ['class'] = ActivityClassif::get();
        return view('admin/activity/index',$data);
    }

    public function getPageRows(Request $request){
        $status = intval($request->input('status'));
        $title = strval($request->input('title'));
        $datetime = $request->input('datetime');
        $query = Activity::with('class');
        $time = date('Y/m/d H:i:s',time());
        if ($status>-1) {
            $query->where('status',$status);
        }
        if($title){
            $query->where('title',$title);
        }
        if($datetime){
            $query->whereDate('start_time','=',$datetime);
        }
        $rows =$query->paginate(20);
        foreach ($rows as $key => $row) {
            $row->count = ActivityJoin::where('activity_id',$row->id)->count();
            $row->statusName = $row->getStatusName();
        }
        $data =[
            'rows'=>$rows,
        ];
        return $this->response($data);
    }

    public function getCreate(){
        $row = new Activity;
        $data ['class'] = ActivityClassif::get();
        $data ['row'] = $row;
        return view('admin/activity/insert',$data);
    }

    public function edit(Request $request){
        $id =  intval($request->input('id'));
        $row = Activity::find($id);
        $row->pic1 = unserialize($row->pic1);
        $data ['row'] = $row;
        $data ['class'] = ActivityClassif::get();
        return view('admin/activity/insert',$data);
    }

    public function store(Request $request){
        $id  = intval($request->input('id'));
        if ($id) {
            $activity = Activity::find($id);
        }else{
            $activity = new Activity;
        }

        if ($request->exists('pic')) {
            $pic1 = trim($request->input('pic'),'|');
            $activity->pic1 = serialize(explode('|',$pic1));
        }
        if ($request->exists('title')) {
            $activity->title = strval($request->input('title'));
        }
        if ($request->exists('user_number')) {
            $activity->user_number = intval($request->input('user_number'));
        }
        if ($request->exists('start_time')) {
            $activity->start_time = $request->input('start_time');
        }
        if ($request->exists('end_time')) {
            $activity->end_time = $request->input('end_time');
        }
        if ($request->exists('introduction')) {
            $activity->introduction = safe2($request->input('introduction'));
        }
        if ($request->exists('content')) {
            $activity->content = safe2($request->input('content'));
        }
        if ($request->exists('notes')) {
            $activity->notes = safe2($request->input('notes'));
        }
        if ($request->exists('class_id')) {
            $activity->class_id = intval($request->input('class_id'));
        }
        if ($request->exists('price')) {
            $activity->price = $request->input('price',0);
        }
        if ($request->exists('place')) {
            $activity->place = safe2($request->input('place'));
        }
        if ($request->exists('host')) {
            $activity->host = safe2($request->input('host'));
        }
        if ($request->exists('status')) {
            $activity->status = intval($request->input('status'));
            if ($request->input('status')==10) {
                $activity->publish_time = safe2($request->input('publish_time'));
            }
        }
        if ($request->exists('look_back')) {
            $activity->look_back = safe2($request->input('look_back'));
        }
        $activity->Save();
        return $this->response('ok');

    }

    //修改发布状态
    public function postStatusChange(Request $request,$id){
        $status =  intval($request->input('status'));
        $row = Activity::find($id);
        if (empty($row)) {
            return $this->error('活动不存在');
        }
        $row->status = $status;
        $row->save();
        return $this->response('ok');
    }

    //修改发布状态
    public function postPublish(Request $request,$id){
        $is_publish = intval($request->input('is_publish'));
        $row = Activity::find($id);
        if (empty($row)) {
            return $this->error('活动不存在');
        }
        $row->is_publish = $is_publish;
        $row->save();
        return $this->response('ok');
    }

    //取消 、添加推荐
    public function postPro($id){
        $row = Activity::find($id);
        if (empty($row)) {
            return $this->error('活动不存在');
        }
        $row->is_pro = $row->is_pro==1 ? 0 : 1;
        $row->save();
        return $this->response('ok');
    }

    //删除
    public function postDel($id)
    {
        $row = Activity::find($id);
        if (empty($row)) {
            return $this->error('活动不存在');
        }
        $row->delete();
        return $this->response('ok');
    }

    //参加列表
    public function activitJoin($id)
    {
        $row = Activity::find($id);
        if (!$row) {
            return $this->error('记录不存在');
        }
        $rows = ActivityJoin::with('user')->where('activity_id',$id)->paginate(15);
        return $this->response($rows);
    }


    //导出
    public function export($id){
        $title = Activity::where('id',$id)->value('title');
        return Excel::download(new ActivityExport(compact('id')), $title.'报名明细.xlsx');
    }

    //分类
    public function class(){
        $rows = ActivityClassif::paginate(20);
        foreach($rows as $row){
            $row->count = Activity::where('class_id',$row->id)->count('id');
        }
        $data['rows'] = $rows;
        return view('admin/activity/class',$data);
    }

    //删除分类
    public function classDel(Request $request){
        $id = intval($request->input('id'));
        $n = Activity::where('class_id',$id)->count('id');
        if ($n > 0) {
            return $this->error('该分类下存在活动记录，不可删除');
        }
        ActivityClassif::where('id',$id)->delete();
        return $this->response($id);
    }
    //添加
    public function classInsert(Request $request){
        $name = safe2($request->input('name'));
        $n = ActivityClassif::where('name',$name)->count();
        if ($n > 0) {
            return $this->error('已存在该分类');
        };
        $row = new ActivityClassif;
        $row->name = $name;
        $row->save();
        return $this->response($row);
    }
}
